﻿@page "/Account/Login"
@layout LoginLayout
@using BootstrapBlazor.Components
@using System.Diagnostics.CodeAnalysis;
@using BootstrapBlazorApp.Shared.Data.SYS;
@using System.Web;
@using WebApiClient;
@inject MessageService msg
@inject NavigationManager nav
@inject Blazored.LocalStorage.ILocalStorageService storage

<div class="wrap">
    <div class="container">
        <form method="post" class="form-signin">
            <h2 class="form-signin-heading">登录</h2>
            <div class="login-wrap">
                <BootstrapInputGroup>
                    <BootstrapInputGroupIcon Icon="fa fa-fw fa-user" />
                    <BootstrapInput TValue="string" name="userName" maxlength="16" IsAutoFocus="true" PlaceHolder="用户名" @bind-Value="LoginModel.Account" />
                </BootstrapInputGroup>
                <BootstrapInputGroup class="mt-3">
                    <BootstrapInputGroupIcon Icon="fa fa-fw fa-lock" />
                    <BootstrapPassword name="password" PlaceHolder="密码" @bind-Value="LoginModel.Password" />
                </BootstrapInputGroup>

                <Button class="btn-login mt-3" ButtonType="ButtonType.Button" Size="Size.Large" IsBlock="true" Color="Color.Primary" OnClick="DoLogin" Text="登 录">
                </Button>
            </div>
        </form>
    </div>
</div>

@code {
    [NotNull]
    private LoginModel? LoginModel { get; set; } = new LoginModel();

    /// <summary>
    /// 当前Token信息
    /// </summary>
    [CascadingParameter]
    private ITokenContext? Context { get; set; }

    private async Task DoLogin()
    {
        if (string.IsNullOrEmpty(LoginModel.Account))
        {
            await msg.Show(new MessageOption()
                {
                    Color = Color.Danger,
                    Content = "用户名不能为空"
                });
            return;
        }

        if (string.IsNullOrEmpty(LoginModel.Password))
        {
            await msg.Show(new MessageOption()
                {
                    Color = Color.Danger,
                    Content = "密码不能为空"
                });
            return;
        }

        var model = await Request.System.LoginAsync(LoginModel);
        if (model.succeeded)
        {
            var user = new CurrentUser()
                {
                    Account = model.data.Account,
                    UserName = model.data.UserName
                };
            await storage.SetItemAsync<CurrentUser>(ClientAppKey.CurrentUser, user);
            //设置Session
            Context.SetSession<CurrentUser>(ClientAppKey.CurrentUser, user);
            nav.NavigateTo("/");
        }
        else
        {
            await msg.Show(new MessageOption()
                {
                    Color = Color.Danger,
                    Content = model.errors
                });
            return;
        }

    }

    /// <summary>
    /// OnInitializedAsync 方法
    /// </summary>
    /// <returns></returns>
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
    }
}
